package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.igeekhome.biz.ICustomerInfoService;
import com.igeekhome.biz.ICustomerServiceService;
import com.igeekhome.biz.ISessionMessageService;
import com.igeekhome.biz.impl.SessionServiceImpl;
import com.igeekhome.pojo.CustomerInfo;
import com.igeekhome.pojo.CustomerService;
import com.igeekhome.pojo.SessionMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会话管理 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/sessionMessage")

public class SessionMessageController {

    @Autowired
    private ISessionMessageService iSessionMessageService;
    @Autowired
    private ICustomerInfoService iCustomerInfoService;

    @RequestMapping("/lishiduihua")
    public String lishihuihua(Model model, HttpSession session) {

        QueryWrapper<SessionMessage> qw = new QueryWrapper<>();
        CustomerService customerService = (CustomerService) session.getAttribute("cs");
        qw.eq("customerserviceid", customerService.getCustomerserviceid());


        List<SessionMessage> messageList = this.iSessionMessageService.list(qw);
        model.addAttribute("messageList", messageList);


        List<CustomerInfo> nameLists1 =new ArrayList<>();
        for (SessionMessage messList : messageList) {
            QueryWrapper<CustomerInfo> qw1 = new QueryWrapper<>();
            qw1.eq("id", messList.getCustomerid());


           CustomerInfo customerInfo=iCustomerInfoService.getOne(qw1);

           int flag=0;

           for(CustomerInfo name:nameLists1){
              if(name.getId()==customerInfo.getId()){
               flag=1;
               }
           }

           if(flag==0) {
               nameLists1.add(customerInfo);
           }
        }
        model.addAttribute("nameLists1",nameLists1);

        return "/lishiduihua";
    }

    @RequestMapping("/liaotian")
    public String sendmessage(Model model, HttpSession session,@RequestParam("ID") int ID,@RequestParam("NAME")String NAME) {
        QueryWrapper<SessionMessage> qw = new QueryWrapper<>();
        CustomerService customerService = (CustomerService) session.getAttribute("cs");

        System.out.println(customerService.getCustomerserviceid());
        System.out.println(ID);

        qw.eq("customerserviceid", customerService.getCustomerserviceid());
        qw.eq("customerId", ID);
            session.setAttribute("NAME", NAME);
            session.setAttribute("cusSerID", customerService.getCustomerserviceid());
            session.setAttribute("cusID", ID);
            List<SessionMessage> messageList2 = this.iSessionMessageService.list(qw);
            model.addAttribute("messageList2", messageList2);

            return "huihuajilu";
        }


    @RequestMapping("/insertmessage")
    public String insertmessage(HttpSession session,@RequestParam("cusID") Integer cusID,@RequestParam("cusSerID") Integer cusSerID,@RequestParam("value")String value){

        LocalDateTime newLDT = LocalDateTime.now();

        int year = newLDT.getYear();//年
        int month = newLDT.getMonth().getValue();//月
        int dayOfMonth = newLDT.getDayOfMonth();//日
        int hour = newLDT.getHour();//时
        int minute = newLDT.getMinute();//分
        int second = newLDT.getSecond();
        SessionMessage sessionMessage=new SessionMessage(cusSerID,false,newLDT,value,cusID);
         boolean i= iSessionMessageService.save(sessionMessage);
       String name=iCustomerInfoService.getById(cusID).getRealname();

session.setAttribute("name",name);
        return "redirect:/sessionMessage/liaotian?ID="+cusID+"&NAME="+name;
    }

}


